#ifndef _CIRC_BUF_H_
#define _CIRC_BUF_H_

#include "windows.h"

#define PUCHAR PBYTE
#define init_mutex		InitializeCriticalSection
#define delete_mutex	DeleteCriticalSection
#define lock_mutex	EnterCriticalSection
#define unlock_mutex	LeaveCriticalSection

class CCircularBuf
{
private:
    unsigned long	m_dwReadPointer;	// read pointer start from 0
    unsigned long	m_dwWritePointer;	// write pointer start from zero
    unsigned long	m_dwSize;			// size of circular buffer
    BOOL		m_bEmpty;			// tag of whether the buffer is empty
    BOOL		m_bOverflow;		// tag of whether the buffer is overflow
    BOOL		m_bOverWrite;		// tag of whether overwrite on overflow is allowed
    CRITICAL_SECTION 	m_CS;		// critical section to protect data access
    PUCHAR	m_pchBuf;
    unsigned long APKGetFreeSpace(void);

public:
    CCircularBuf(DWORD dwSize, BOOL bOverwrite = TRUE);
    ~CCircularBuf();
	unsigned long GetDataSize(void);
    unsigned long Read(PUCHAR pBuf, DWORD dwBufLength);
    unsigned long Write(PUCHAR pBuf, DWORD dwBufLength);
    void Reset(void);
    BOOL IsEmpty(void);
    BOOL IsFull(void);
    BOOL IsOverflow(void);
    void ClearOverflow(void);
};

#endif// _CIRC_BUF_H_
